//
//  CZStatusCell.swift
//  GZWeibo007
//
//  Created by Apple on 15/12/20.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit

// 微博cell基本类.原创cell和转发cell都是继承自CZStatusCell
class CZStatusCell: UITableViewCell,JCPictureLayoutDelegate {
    
    
    // MARK: - 属性
    /// 配图宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    /// 配图高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    var picCount = 0
    
    // cell显示内容需要模型
    var status: CZStatus? {
        didSet {
            // 当控制器给cell设置模型后,顶部视图显示数据也是依赖模型
            topView.status = status
            
            // 设置微博的内容
            contentLabel.text = status?.text
            
            // 将模型设置给配图视图.
            pictureView.status = status
            //*************************************//
            
            picCount = status?.pictureURLs?.count ?? 0
            
            //*************************************//
            
        }
    }
    
    func pictureLayout(layout: JCPictureLayout) -> (CGFloat) {
//        print("picCount == \(picCount)")
        var col = 0
        switch picCount{
        
        case 0: col = 0
        case 1: col = 1
        case 2,4: col = 2
        default: col = 3
        
        }
        return CGFloat(col)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 准备UI
        preapreUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 准备UI
    func preapreUI() {
        // 1. 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
//        pictureView.backgroundColor = UIColor.blueColor()
        // 2. 添加约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(contentView)
            make.height.equalTo(53).priorityHigh()
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(5).priorityHigh()
            make.leading.equalTo(topView).offset(8)
            make.trailing.equalTo(topView).offset(-8)
        }
        
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.leading.equalTo(topView).offset(8)
//            make.trailing.equalTo(topView).offset(-8)
//            make.top.equalTo(contentLabel.snp_bottom).offset(8)
//            make.height.equalTo(200)
//        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(contentView)
            //            make.height.equalTo(44)
            make.top.equalTo(pictureView.snp_bottom).offset(8)
            make.height.equalTo(36)
        }
    }
    
    
    // MARK: - 懒加载
    /// 顶部视图
    private lazy var topView: CZTopView = CZTopView()
    
    /// 微博文本内容
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        // 字体大小
        label.font = UIFont.systemFontOfSize(14)
        
        // 字体颜色
        label.textColor = UIColor.darkGrayColor()
        
        // 换行
        label.numberOfLines = 0
        
        // 设置折行文本宽度(换行的宽度)
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 8
        
        return label
    }()
    
    /// 微博配图视图
    lazy var pictureView: CZStatusPictureView = {
    
        let pictureView = CZStatusPictureView()
        
        pictureView.layout.delegate = self
        
        return pictureView
    }()
    
    /// 底部视图
    lazy var bottomView: CZStatusBottomView = CZStatusBottomView()
}
